<style type="text/css">
A:link {text-decoration: underline; color: #BCC1DD;}
A:visited {text-decoration: none; color: #BCC1DD;}
A:active {text-decoration: none}
A:hover {font-size:2430; font-weight:bold; color: #0099FF;}
</style>

<script src="<?php echo base_url(); ?>js/jquery.js" type="text/javascript"></script>
<script src="<?php echo base_url(); ?>js/jquery.boxy.js" type="text/javascript"></script>
<link rel="stylesheet" href="<?php echo base_url(); ?>css/boxy.css" type="text/css" />

<?php
	if(isset($user_data)) {
		echo '<h1>' . $user_data['account'] . '\'s Profile</h1> </br>';
		if($user_data['account'] == $this->session->userdata('account')) { //Add my challenges only if this is my account
?>

<script src="<?php echo base_url(); ?>js/challenge.js" type="text/javascript" ></script>

<div style="position: relative" id="my_challenges">
	<fieldset style="position: relative">

		<legend>Your Pending Challenges</legend>
		<?php
			$other_score;
			if(count($user_data['challenges']) > 0){
				foreach($user_data['challenges'] as $challenge){

					
					$other_player = $user_data['account'] == $challenge->PLAYER1 ? $challenge->PLAYER2 : $challenge->PLAYER1;
					$other_score = $profile_model->get_field($other_player, 'score');
					if($challenge->STATUS == 'pending'){

						// Check if challenge has timed out
						if($this->profile_model->timeout_check($challenge)) {


							if($user_data['account'] == $challenge->PLAYER1){
								echo 'Waiting for ' . $other_player .' to accept challenge' . br(1);
							}
							else{
								$javascript = 'acceptChallenge(\'' . $challenge->get_id() . '\'); new Boxy(\'#hidden_accept_form\', {modal : true});';
								echo 'Challenge with ' . $challenge->PLAYER1.' is pending | ';
								echo '<a style="cursor:pointer" onclick="'.$javascript.'">ACCEPT</a> |' . br(1);
							}

						} else {
							echo 'Challenge with ' . $other_player .' has expired' . br(1);
						}
					}
					else{
						$javascript = 'reportGame(\'' . $challenge->get_id() . '\', \'' . $challenge->PLAYER1 .
									  '\', \'' . $challenge->PLAYER2 . '\'); new Boxy(\'#hidden_report_form\','.
									  ' {modal : true, draggable : false, closeable : false, title : \'Result for game '.
									  ($challenge->GAMES_PLAYED+1).' ('.$challenge->PLAYER1.' vs '.$challenge->PLAYER2. ')\'});';

						echo 'Challenge with ' . $other_player . ' ('.$challenge->GAMES_PLAYED.'/'.$challenge->NUM_GAMES.') | ';
						echo '<a style="cursor:pointer" onclick="'.$javascript.'">REPORT </a> | ';
						echo anchor('game/show/' . $challenge->get_id(), 'VIEW') . '</a>' . ' |' . br(1);
					}
				}
			}
			else echo 'No pending challenges.';
		?>
	</fieldset>
</div>

<?php
		}
?>

<div style="position: relative" id="profile_picture">
	<fieldset style="position: relative">
		<legend>Profile Picture</legend>
		<?php

			if(isset($picture))
				echo "$picture";
			else
				echo '<img src="../../images/Marine.jpg" alt="profile picture" width="160" height="160"/>';

		?>
	</fieldset>
</div>

<div style="position: relative" id="personal_info">
	<fieldset style="position: relative">
		<legend>Personal Information</legend>
		<?php
			echo 'Name: ' . $user_data['first_name'] . ' ' . $user_data['last_name'] . '<br />';
			echo 'Birthday: ' . $user_data['birthday'] . '<br />';
			echo 'Email: ' . $user_data['email'].br(2);

			if(strlen($user_data['bnet']) > 0){
				echo anchor($user_data['bnet'], 'Battle.net Profile').'<br/><br/>';
			} else {
				//TODO: Remove when Bnet parser goes live
				echo br(2).'User has not provided a Battle.net URL or URL is invalid.';
			}
		?>


	</fieldset>
</div>

<!--
<div style="position: relative" id="bnet_info">
		<?php
			echo 'Battle.net Statistics <br />';
			
			if(isset($bnetinfo)){
				if(is_array($bnetinfo)){
					?>
						<table border="6" id="profile_table">
							<tr id = "profile_table_header"><th>Name</th><th>League</th><th>Score</th><th>Wins</th><th>Losses</th><th>Win Percentage</th></tr>
							<tr>
								<td><?php echo $bnetinfo['name'];?></td>
								<td><?php echo $bnetinfo['league'];?></td>
								<td><?php echo $bnetinfo['score'];?></td>
								<td><?php echo $bnetinfo['wins'];?></td>
								<td><?php echo $bnetinfo['losses'];?></td>
								<td><?php echo $bnetinfo['ratio'].'%';?></td>
							</tr>
						</table>
					<?php
				}else
				echo $bnetinfo;
			}
			else{
				echo br(2).'User has not provided a Battle.net URL or URL is invalid.';
			}
		?>
</div>
-->

<div style="position: relative" id="ladder_info">

		<?php
			echo $user_data['account'] . '\'s MSC Ladder Statistics <br />';
		?>
			<table border="6" id="profile_table2">
				<tr id = "profile_table_header2"><th>Rank</th><th>Score</th><th>Wins</th><th>Losses</th><th>Win Percentage</th></tr>
					<tr>
					<td><?php echo $user_data['rank'];?></td>
					<td><?php echo $user_data['score'];?></td>
					<td><?php echo $user_data['ladder_wins'];?></td>
					<td><?php echo $user_data['ladder_losses'];?></td>
					<td><?php
							$total_games = $user_data['ladder_wins'] + $user_data['ladder_losses'];
							echo $total_games == 0 ? '0%' : (round(100*$user_data['ladder_wins']/$total_games, 1)).'%';
						?></td>
				</tr>
			</table>
</div>


<div style="position: relative" id="challenge_info">

		<?php
			echo $user_data['account'] . '\'s Recent Matches <br />';
		?>

		<?php

			// Set table template
			$tmpl = array (
				'table_open' => '<table border="6" style="position: relative" style="backgroud-color: #101225 ;" color="BCC1DD" id="mainTable_roster_profile">'
			);

			$table_library->set_template($tmpl);

			$recent_games_data = $profile_model->get_recent_games($user_data['account']);
			

			foreach ($recent_games_data as $key => $val){
				$recent_games_data[$key]['game_set_id'] = '<a href="'.base_url().'game/show/'.$val['game_set_id'].'">'.$val['game_set_id'].'</a>';
				$recent_games_data[$key]['player1'] = '<a href="'.base_url().'profile/show/'.$val['player1'].'">'.$val['player1'].'</a>';
				$recent_games_data[$key]['player2'] = '<a href="'.base_url().'profile/show/'.$val['player2'].'">'.$val['player2'].'</a>';
			}

			// Generate table of completed challenges (game sets)
			$table_library->set_heading('ID', 'Player 1', 'Player 2', 'P1 Wins', 'P2 Wins');
			echo $table_library->generate($recent_games_data);
		  	$table_library->clear();

		?>

</div>

<?php
	} else {
		echo '<h1>User Not Found</h1>';
	}
?>

<div id='hidden_accept_form' style='display: none; color: black'>
<?php
	echo 'Are you sure you want to accept challenge?' . br(1);
	echo 'Map choice for first game will be requested here.' . br(2);

	echo form_open('/challenge/accept_challenge', array('class' => 'update_game_form', 'id' => 'accept_form'));
	echo form_hidden('set_id', '');
	echo form_submit('submit', 'Accept');
	echo form_submit('submit', 'Decline');
?>
<input type="button" onclick="Boxy.get(this).hide();" id="accept_close" value="Cancel" />
<?php echo form_close(); ?>
</div>

<div id='hidden_report_form' style='display: none; color: black'>
<?php
	//Load config variables
	$this->load->config('challenge_settings');
	$maps = $this->config->item('active_maps');

	$this->load->config('upload_settings', TRUE);
	$upload_settings = $this->config->item('upload_settings');

	//Form stuff follows
	$form_attributes = array ('class' => 'update_game_form', 'id' => 'report_form', 'onsubmit' => 'return checkReportForm(this);');
	echo br(1) . form_open_multipart('challenge/report_game', $form_attributes);
	echo form_hidden('set_id', '');
	echo form_hidden('player1', '');
	echo form_hidden('player2', '');
	echo form_label('Map: ') . form_dropdown('map', $maps, set_value('map')) .br(2);
	echo form_label('Winner: ') . form_dropdown('winner', array('unplayed' => ''), set_value('winner')) .br (2);
	echo form_label('Replay File: ') . form_upload('replay') . br(4);
	echo form_submit('upload', 'Report Results');
	echo form_reset('reset', 'Clear Entries');
?>
<input type="button" onclick="Boxy.get(this).hide();" id="report_close" value="Cancel" />
<?php echo form_close(); ?>
</div>
